﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace FourthWallProject.GameObjects.Screens
{
    class MenuScreen : GameScreen
    {
        class MenuItem
        {
            public MenuItem(string Text,int number, bool Selected = false)
            {
                this.Text = Text;
                this.Selected = Selected;
                this.X = 100;
                this.Y = 100 + (number * 40);
            }
            public Vector2 Size { get { return GameState.MenuFont.MeasureString(Text); } }
            public int X { get; set; }
            public int Y { get; set; }
            public string Text { get; set; }
            public bool Selected { get; set; }
        }

        List<MenuItem> MenuItems = new List<MenuItem>();
        public MenuScreen(Game game)
            : base(game)
        {
            MenuItems.Add(new MenuItem("Start Game",0,true));
            MenuItems.Add(new MenuItem("Load Game", 1));
            MenuItems.Add(new MenuItem("Settings", 2));
            MenuItems.Add(new MenuItem("About", 3));
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (var menuItem in MenuItems)
            {
                menuItem.Selected = false;
                // TODO: Add your update code here
                if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                {
                    if (new Rectangle(Mouse.GetState().X - 3, Mouse.GetState().Y - 3, 6, 6).Intersects(new Rectangle(menuItem.X, menuItem.Y, (int)menuItem.Size.X, (int)menuItem.Size.Y)))
                    {
                        menuItem.Selected = true;
                        if (menuItem.Text == "Start Game")
                        {
                            var c = new LevelScreen(this.Game);
                            ScreenManager.AddScreen(c);
                            GameState.LevelScreen = c;
                            ScreenManager.PauseScreen(this);
                        }
                    }
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            GraphicsDevice.Clear(Color.Green);
            spriteBatch.Begin();
            foreach (MenuItem menuItem in MenuItems)
            {
                if(menuItem.Selected)
                    spriteBatch.DrawString(GameState.MenuFont, menuItem.Text, new Vector2(menuItem.X, menuItem.Y), Color.White);
                else
                    spriteBatch.DrawString(GameState.MenuFont, menuItem.Text, new Vector2(menuItem.X, menuItem.Y), Color.Red);

            }
            spriteBatch.End();
            base.Draw(gameTime, spriteBatch);
        }
    }
}
